#!/bin/sh

normal()
{
    printf "\033[0;34mMode Normal\033[0m\n"
    i=1
    while [ -f "./tests/test$i.kc" ]
    do
	./kooc "./tests/test$i.kc"  > "./tests/test$i.c.out"
	if [ -f "./tests/test$i.c" ]
	then
	    diff --suppress-common-lines "./tests/test$i.c" "./tests/correction/test$i.c" >> "./tests/test$i.c.out" 2>&1
	    if [ $? != 0  ] 
	    then
		printf "\033[0;31mtest$i.kc Failed\033[0m\n"
		cat ./tests/test$i.c.out
	    else
		printf "\033[0;32mtest$i.kc Passed\033[0m\n"
	    fi
	else
	    printf "\033[0;31mtest$i.kc KOOC error\033[0m\n"
	    cat ./tests/test$i.c.out
	fi
	i=`expr $i + 1` > /dev/null
    done
    
    i=1
    while [ -f "./tests/test$i.kh" ]
    do
	./kooc "./tests/test$i.kh"  > "./tests/test$i.h.out"
	if [ -f "./tests/test$i.h" ]
	then
	    diff --suppress-common-lines "./tests/test$i.h" "./tests/correction/test$i.h" >> "./tests/test$i.h.out" 2>&1
	    if [ $? != 0  ] 
	    then
		printf "\033[0;31mtest$i.kh Failed\033[0m\n"
		cat ./tests/test$i.h.out
	    else
		printf "\033[0;32mtest$i.kh Passed\033[0m\n"
	    fi
	else
	    printf "\033[0;31mtest$i.kh KOOC error\033[0m\n"
	    cat ./tests/test$i.h.out
	fi
	i=`expr $i + 1` > /dev/null
    done
}

gcc()
{
    printf "\033[0;34mMode GCC\033[0m"
    i=1
    while [ -f "./tests/test$i.kc" ]
    do
	./kooc "./tests/test$i.kc"  > "./tests/test$i.c.out"
	if [ -f "./tests/test$i.c" ]
	then
	    diff --suppress-common-lines "./tests/test$i.c" "./tests/correction/test$i.c" >> "./tests/test$i.c.out" 2>&1
	    if [ $? != 0  ]
	    then
		printf "\033[0;31mtest$i.kc Failed\033[0m\n"
	    else
		printf "\033[0;32mtest$i.kc Passed\033[0m\n"
		gcc -c ./tests/test$i.c
	    fi
	else
	    printf "\033[0;31mtest$i.kc KOOC error\033[0m\n"
	fi
	i=`expr $i + 1` > /dev/null
    done
    
    i=1
    while [ -f "./tests/test$i.kh" ]
    do
	./kooc "./tests/test$i.kh"  > "./tests/test$i.h.out"
	if [ -f "./tests/test$i.h" ]
	then
	    diff --suppress-common-lines "./tests/test$i.h" "./tests/correction/test$i.h" >> "./tests/test$i.h.out" 2>&1
	    if [ $? != 0  ] 
	    then
		printf "\033[0;31mtest$i.kh Failed\033[0m\n"
		cat ./tests/test$i.h.out
	    else
		printf "\033[0;32mtest$i.kh Passed\033[0m\n"
	    fi
	else
	    printf "\033[0;31mtest$i.kh KOOC error\033[0m\n"
	fi
	i=`expr $i + 1` > /dev/null
    done
}


kooc()
{
    printf "\033[0;34mMode KOOC\033[0m\n"
    i=1
    while [ -f "./tests/test$i.kc" ]
    do
	./kooc "./tests/test$i.kc"  > "./tests/test$i.c.out"
	if [ -f "./tests/test$i.c" ]
	then
	    diff --suppress-common-lines "./tests/test$i.c" "./tests/correction/test$i.c" >> "./tests/test$i.c.out" 2>&1
	    if [ $? != 0  ] 
	    then
		printf "\033[0;31mtest$i.kc Failed\033[0m\n"
	    else
		printf "\033[0;32mtest$i.kc Passed\033[0m\n"
	    fi
	else
	    printf "\033[0;31mtest$i.kc KOOC error\033[0m\n"
	    cat ./tests/test$i.c.out
	fi
	i=`expr $i + 1` > /dev/null
    done
    
    i=1
    while [ -f "./tests/test$i.kh" ]
    do
	./kooc "./tests/test$i.kh"  > "./tests/test$i.h.out"
	if [ -f "./tests/test$i.h" ]
	then
	    diff --suppress-common-lines "./tests/test$i.h" "./tests/correction/test$i.h" >> "./tests/test$i.h.out" 2>&1
	    if [ $? != 0  ] 
	    then
		printf "\033[0;31mtest$i.kh Failed\033[0m\n"
	    else
		printf "\033[0;32mtest$i.kh Passed\033[0m\n"
	    fi
	else
	    printf "\033[0;31mtest$i.kh KOOC error\033[0m\n"
	    cat ./tests/test$i.h.out
	fi
	i=`expr $i + 1` > /dev/null
    done
}

clean()
{
    printf "\033[0;34mCleaning\033[0m\n"
    rm -rf *~
    rm -rf \#*
    rm -rf ./tests/*~
    rm -rf ./tests/\#*
    rm -rf ./tests/correction/*~
    rm -rf ./tests/correction/\#*
    rm -rf ./tests/*.c
    rm -rf ./tests/*.h
    rm -rf ./tests/*.ktree
    rm -rf ./tests/*.out
}

help()
{
   printf "\033[0;34mhelp\033[0m\n"
   printf "\033[0;34m./laucher_test =>  normal display kooc error , diff error \033[0m\n"
   printf "\033[0;34m./laucher_test kooc => display kooc error only +\033[0m\n"
   printf "\033[0;34m./laucher_test gcc => compile and display gcc error only \033[0m\n"
   printf "\033[0;34m./laucher_test clean => cleanning\033[0m\n"
   printf "\033[0;34m./laucher_test help => display this help\033[0m\n"
}

case $* in
"") clean;normal;;
"gcc")clean;gcc;;
"kooc")clean;kooc;;
"clean")clean;;
"help" | "H" | "h" | "?")help;;
esac